<?php

App::uses('AppController', 'Controller');

/**
 * Promotions Controller
 *
 * @property Promotion $Promotion
 */
class PromotionsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Promotion->recursive = 0;
        $this->set('promotions', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Promotion->exists($id)) {
            throw new NotFoundException(__('Invalid promotion'));
        }
        $options = array('conditions' => array('Promotion.' . $this->Promotion->primaryKey => $id));
        $this->set('promotion', $this->Promotion->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Promotion->create();
            if ($this->Promotion->save($this->request->data) && ($this->updatePromotionShops($this->Promotion->getId()))
            ) {
                $this->Session->setFlash(__('La Promocion ha sido creada con exito'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Error, Promocion no ha sido creada. Intente otra vez.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {

        $this->Promotion->id = $id;

        if (!$this->Promotion->exists($id)) {
            throw new NotFoundException(__('Error, Promocion no valida'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Promotion->save($this->request->data)) {
                $this->Session->setFlash(__('La Promocion ha sido actualizada con exito'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Error, Promocion no guardada. Intente otra vez.'));
            }
        } else {
            $options = array('conditions' => array('Promotion.' . $this->Promotion->primaryKey => $id));
            $this->request->data = $this->Promotion->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Promotion->id = $id;
        if (!$this->Promotion->exists()) {
            throw new NotFoundException(__('Invalid promotion'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Promotion->delete()) {
            $this->Session->setFlash(__('Promotion deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Promotion was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function updatePromotionShops($idPromotion = null) {

        $shops = $this->Promotion->PromotionShop->Shop->find('all', array('fields' => array('Shop.id')));
        foreach ($shops as $shop) {

            $this->Promotion->PromotionShop->saveAll(array(
                'PromotionShop' => array('shop_id' => $shop['Shop']['id'], 'promotion_id' => $idPromotion)
            ));
        }
        return true;
    }

}
